﻿using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Storage;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using System;
using System.Reflection;
using System.Reflection.Metadata;

namespace DX.DbStorage.EF
{
    public class DbStorageContext : DbContext, IDbStorage
    {

        public DbStorageContext(IServiceProvider serviceProvider, DbContextOptions options) : base(options)
        {
            this.serviceProvider = serviceProvider;
        }
        private readonly IServiceProvider serviceProvider;
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            var option = serviceProvider.GetRequiredService<IOptions<EFDbStorageOptions>>().Value;
            Assembly assembly = option.EntytyAssembly;
            var types = assembly.GetTypes();
        }
        IDbContextTransaction transaction;
        public void Transaction()
        {
            transaction = this.Database.BeginTransaction();
        }
        public void Commit()
        {

            this.SaveChanges();
            transaction?.Commit();
        }

        public void Rollback()
        {
            transaction?.Rollback();
        }
    }
}
